package com.example.testproject.call_back;

import android.content.Context;
import android.graphics.Canvas;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.example.testproject.adapter.CardStackAdapter;
import com.example.testproject.bean.CardStackBean;
import com.example.testproject.rv_layout_manager.CardStack2LayoutManager;

/**
 * @ClassName: SlideCardStackCallBack2
 * @Author: android 超级兵
 * @CreateDate: 11/6/21$ 2:47 PM$
 * TODO
 */
public class SlideCardStackCallBack2<T> extends ItemTouchHelper.SimpleCallback {
    private final CardStackAdapter<T> mAdapter;

    public SlideCardStackCallBack2(CardStackAdapter<T> mAdapter) {
        super(0, 15);
        this.mAdapter = mAdapter;
    }

    @Override
    public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
        return false;
    }

    @Override
    public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
        // 当前滑动的View
        int layoutPosition = viewHolder.getLayoutPosition();
        Log.i("szjLayoutPosition", "layoutPosition:" + layoutPosition);
        // 删除当前滑动的元素
        CardStackBean<T> bean = mAdapter.getData().remove(layoutPosition);

        // 添加到集合第0个位置 造成循环滑动的效果
        mAdapter.addData(0, bean);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
        int maxDistance = recyclerView.getWidth() / 2;

        // dx = 当前滑动x位置
        // dy = 当前滑动y位置
        //sqrt 开根号
        double sqrt = Math.sqrt((dX * dX + dY * dY));

        Log.i("szjDxDy", "Dx:" + dX + "\tDy:" + dY);

        // 放大系数
        double scaleRatio = sqrt / maxDistance;

        if (scaleRatio > 1.0) {
            scaleRatio = 1.0;
        }

        int childCount = recyclerView.getChildCount();
        // 循环所有数据
        for (int i = 0; i < childCount; i++) {
            View view = recyclerView.getChildAt(i);

            int valueDip = toDip(view.getContext(), 20f);

            /*
             * 作者:android 超级兵
             * 创建时间: 11/6/21 3:29 PM
             * TODO
             *   childCount - 1 =  itemView总个数
             *    childCount - 1 - i = itemView总个数 - i = 从最后一个开始
             *
             * 假设 childCount - 1 = 7
             *     i累加
             *     那么level = childCount - 1 - 0 = 7
             *     那么level = childCount - 1 - 1 = 6
             *     那么level = childCount - 1 - 2 = 5
             *     那么level = childCount - 1 - 3 = 4
             *     那么level = childCount - 1 - 4 = 3
             *      。。。。
             */
            int level = childCount - 1 - i;
            if (level > 0) {
                // 最大显示叠加个数:CardStack2LayoutManager.MAX_SHOW_COUNT = 4
                if (level < CardStack2LayoutManager.MAX_SHOW_COUNT - 1) {
                    // 缩放比例: CardStack2LayoutManager.SCALE = 0.05
                    float scale = CardStack2LayoutManager.SCALE;

                    // valueDip * level  = 原始平移距离
                    // scaleRatio * valueDip = 平移系数
                    // valueDip * level - scaleRatio * valueDip = 手指滑动过程中的Y轴平移距离
                    // 因为是Y轴,所以向上平移是 - 号
                    view.setTranslationY((float) (valueDip * level - scaleRatio * valueDip));

                    // 1 - scale * level = 原始缩放大小
                    // scaleRatio * scale = 缩放系数
                    // 因为是需要放大,所以这里是 + 号
                    view.setScaleX((float) ((1 - scale * level) + scaleRatio * scale));
                    view.setScaleY((float) ((1 - scale * level) + scaleRatio * scale));
                }
            }
        }
    }

    private int toDip(Context context, float value) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, context.getResources().getDisplayMetrics());
    }

    // 设置回弹距离
    @Override
    public float getSwipeThreshold(@NonNull RecyclerView.ViewHolder viewHolder) {
        return 0.3f;
    }


    // 设置回弹时间
//    @Override
//    public long getAnimationDuration(@NonNull RecyclerView recyclerView, int animationType, float animateDx, float animateDy) {
//        return 3000;
//    }
}
